package com.zidian.teacher.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import com.zidian.teacher.utils.DensityUtils;

/**
 * Created by Administrator on 2016/9/8 0008.
 * 用于画评价详情散点图
 */
public class ScatterView extends View {
    private Context context;
    private Paint mPaint;//画笔
    private int startX = 0;//画布的原点X（所有的画图操作，都是基于这个原点的）
    private int startY = 0;//画布的原点Y（所有的画图操作，都是基于这个原点的）
    private static int sideheight = 87;//左边，上面bar的宽度
    private static int sidewidth = 90;//左边，上面bar的宽度
    private static int eachBoxH = 135;//每个格子的高度，设置可以后面根据屏幕对它做了均分
    private static int eachBoxW = 120;//每个格子的宽度，后面根据屏幕对它做了均分
    private static int classTotal = 12;//左边栏总格子数
    private static int dayTotal = 11;//x方向格子数，后面动态分配

    public static final int barBg = Color.argb(255, 159, 159, 159);
    public static final int markerBorder = Color.argb(255, 232, 232, 232);
    public static final String[] YText = {"5", "4.5", "4", "3.5", "3", "2.5", "2", "1.5", "1", "0.5", "0"};

    public ScatterView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (statisticsList.getCount() == 0) {
            eachBoxW = getWidth() / 10;
        } else {
            eachBoxW = getWidth() / statisticsList.getCount();
        }
        eachBoxH = (getHeight() - sideheight) / 12;
        sideheight = eachBoxH / 2;
        printMarker(canvas);
        printLeftBar(canvas);
        printBottomBar(canvas);
        printPonit(canvas);
    }

    private void printMarker(Canvas canvas) {
        mPaint.setColor(markerBorder);

        for (int j = 0; j < classTotal - 1; j++) {
            // 画交线处的十字


            mPaint.setStyle(Paint.Style.STROKE);
            canvas.drawRect(
                    sidewidth,
                    (j + 1) * eachBoxH,
                    eachBoxW * dayTotal,
                    (j + 1) * eachBoxH + 1,
                    mPaint);

        }
    }


    private void printLeftBar(Canvas canvas) {
        mPaint.setColor(barBg);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setTextSize(30);

        Rect textRect1 = new Rect();
        mPaint.getTextBounds("1", 0, 1, textRect1);
        int th = textRect1.bottom - textRect1.top;
        int tw1 = textRect1.right - textRect1.left;
        mPaint.getTextBounds("1.5", 0, 2, textRect1);
        int tw2 = textRect1.right - textRect1.left;

        for (int i = 0; i < classTotal - 1; i++) {
            // 画文字
            int tw = tw1 * 2 + (tw2 - tw1) * (i / 10);
            canvas.drawText(YText[i], sidewidth / 2 - tw / 2, startY
                    + eachBoxH * (i + 1) + th / 2, mPaint);
        }


    }

    private void printBottomBar(Canvas canvas) {
        mPaint.setColor(barBg);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setTextSize(30);

        Rect textRect1 = new Rect();
        mPaint.getTextBounds("1", 0, 1, textRect1);
        int th = textRect1.bottom - textRect1.top;
        int tw1 = textRect1.right - textRect1.left;

        for (int i = 1; i < dayTotal + 1; i++) {
            // 画文字
            canvas.drawText(i + "", startX + sidewidth + eachBoxW * (i - 1) + eachBoxW / 2 - tw1 / 2, eachBoxH * (classTotal - 1) + sideheight + th / 2, mPaint);

        }


    }


    private void printPonit(Canvas canvas) {


        Rect textRect1 = new Rect();
        mPaint.getTextBounds("1", 0, 1, textRect1);
        int th = textRect1.bottom - textRect1.top;
        int tw1 = textRect1.right - textRect1.left;

        for (int i = 1; i < statisticsList.getList().size() + 1; i++) {
            mPaint.setColor(statisticsList.getList().get(i - 1).getColor());
            // 画文字
            int in = ((int) (statisticsList.getList().get(i - 1).getSor() / 1)) * 2;
            int s = ((int) (statisticsList.getList().get(i - 1).getSor() * 10)) % 10;
            int d = ((int) (statisticsList.getList().get(i - 1).getSor() * 100)) % 10;
            canvas.drawCircle(startX + sidewidth + eachBoxW * (i - 1) + eachBoxW / 2 - tw1 / 2, (eachBoxH * 11) - eachBoxH * in - eachBoxH / 5 * s - eachBoxH / 25 * d,
                   DensityUtils.dip2px(context,8), mPaint);// 小圆
        }


    }

    private StatisticsList statisticsList = new StatisticsList();

    public void setData(StatisticsList statisticsList) {
        this.statisticsList = statisticsList;
        invalidate();// 刷新页面
    }

}
